package com.ymatou.iapi.thirdpartysync.testcase;

import org.junit.After;

import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.iapi.thirdpartysync.parameter.*;
import com.ymatou.iapi.thirdpartysync.service.*;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.business.service.ProductManagerCallServiceV3;
import com.ymttest.business.service.ThirdpartysyncCallService;
import com.ymttest.utils.EnvSetup;
/**
*查询推广商品列表Test
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_StopPromotionProduct{
	private static StopPromotionProductBean stoppromotionproductBean;
	private static StopPromotionProductCall stoppromotionproductCall;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("productsellerid"));
	private static String sellerName = EnvSetup.getData("productsellername");
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查询推广商品列表Test");
	}

	@Before
	public void caseUp() {
		stoppromotionproductBean=new StopPromotionProductBean();
		stoppromotionproductCall=new StopPromotionProductCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(stoppromotionproductCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_StopPromotionProduct_001() {
		Logger.start(true,"正在推广中商品停止推广，成功");
		try {
			AddSellerProductRequest addProdReq = ProductManagerCallServiceV3.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addProdReq).get("Data").getAsJsonObject()
					.get("ProductId").getAsString();
			Thread.sleep(3000);
			//添加推广商品
			ThirdpartysyncCallService.insertPromotionProduct(productId, sellerId, 0.03);
			//查询id
			int id = ThirdpartysyncCallService.GetPromotionId(productId, addProdReq.getProduct().getProductName());
			//查询推广stauts
			int beforeStutus = ThirdpartysyncCallService.GetProductStutus(productId);
			Logger.verifyEquals(1, beforeStutus, "验证之前推广中");
			stoppromotionproductBean.setId(id);
			stoppromotionproductCall.setData(stoppromotionproductBean);
			stoppromotionproductCall.callService();
			Thread.sleep(2000);
			int afterStutus = ThirdpartysyncCallService.GetProductStutus(productId);
			Logger.verifyEquals(0, afterStutus, "验证之后为推广");
			Logger.verifyEquals(true, stoppromotionproductCall.getJsonObject().get("success").getAsBoolean(), "验证返回");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_StopPromotionProduct_002() {
		Logger.start(true,"已经停止的商品再次停止推广，失败");
		try {
			AddSellerProductRequest addProdReq = ProductManagerCallServiceV3.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addProdReq).get("Data").getAsJsonObject()
					.get("ProductId").getAsString();
			Thread.sleep(1000);
			//添加推广商品
			ThirdpartysyncCallService.insertPromotionProduct(productId, sellerId, 0.03);
			Thread.sleep(1000);
			//查询id
			int id = ThirdpartysyncCallService.GetPromotionId(productId, addProdReq.getProduct().getProductName());
			Thread.sleep(1000);
			//查询推广stauts
			int beforeStutus = ThirdpartysyncCallService.GetProductStutus(productId);
			Logger.verifyEquals(1, beforeStutus, "验证之前推广中");
			
			stoppromotionproductBean.setId(id);
			stoppromotionproductCall.setData(stoppromotionproductBean);
			stoppromotionproductCall.callService();
			Logger.verifyEquals(true, stoppromotionproductCall.getJsonObject().get("success").getAsBoolean(), "验证返回");
			Thread.sleep(2000);
			int afterStutus = ThirdpartysyncCallService.GetProductStutus(productId);
			Logger.verifyEquals(0, afterStutus, "验证之后为推广");
			stoppromotionproductCall.callService();
			Thread.sleep(2000);
			
			int afterStutus1 = ThirdpartysyncCallService.GetProductStutus(productId);
			Logger.verifyEquals(0, afterStutus1, "验证之后为推广");
			Logger.verifyEquals(true, stoppromotionproductCall.getJsonObject().get("success").getAsBoolean(), "验证返回");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_StopPromotionProduct_003() {
		Logger.start(true,"商品id错误，失败");
		try {
			stoppromotionproductBean.setId(1111111111);
			stoppromotionproductCall.setData(stoppromotionproductBean);
			stoppromotionproductCall.callService();
			Logger.verifyEquals(false, stoppromotionproductCall.getJsonObject().get("success").getAsBoolean(), "验证返回");
			Logger.verifyEquals("推广商品不存在", stoppromotionproductCall.getJsonObject().get("errorMessage").getAsString(), "验证返回");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_StopPromotionProduct_004() {
		Logger.start(true,"商品id为空，失败");
		try {
			stoppromotionproductCall.setData(stoppromotionproductBean);
			stoppromotionproductCall.callService();
			Logger.verifyEquals(false, stoppromotionproductCall.getJsonObject().get("success").getAsBoolean(), "验证返回");
			Logger.verifyIsContains("id:must", stoppromotionproductCall.getJsonObject().get("errorMessage").getAsString(), "验证返回");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
}